package com.zb.activemq.have_two_try;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by dongyuncong on 2017/8/9.
 */
public class ObjectConsumer {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://39.108.78.245:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("ObjectQueue");
            MessageConsumer consumer = session.createConsumer(destination);
            while (true) {
                ObjectMessage message = (ObjectMessage) consumer.receive(500000);
                if (null != message) {
                    System.out.println("Consumer: " + message.getObject());
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }
}
